package com.diankangwo.dao.impl;

import com.diankangwo.dao.CollectionDAO;
import com.diankangwo.domain.Collection;
import com.diankangwo.util.PageSupport;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class CollectionDAOImpl extends BaseDaoImpl<Collection> implements CollectionDAO {

    @Override
    public PageSupport<Collection> getCollectionByUid(String uid, int pageNo, int pageSize, Integer type) {
        PageSupport<Collection> collectionList = new PageSupport<>();
        if (type == null || type == 0) {
            collectionList = findPagination("from Collection conn where conn.uid = ?", pageNo, pageSize, uid);
        } else {
            collectionList = findPagination("from Collection conn where conn.uid = ? and conn.class_id = ?", pageNo, pageSize, uid, type);
        }

        if (collectionList == null || collectionList.getResultData().size() == 0) {
            return null;
        } else {
            return collectionList;
        }
    }

    @Override
    public boolean deleteByCid(Integer goods_id) {
        List<Collection> collectionList = new ArrayList<Collection>();
        collectionList = findByQuery("from Collection conn where conn.goods_id = ?", goods_id);
        try {
            if (collectionList != null) {
                for (Collection collection : collectionList) {
                    delete(collection);
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean isCollection(String uid,Integer goods_id) {
        List<Collection> collectionList = new ArrayList<Collection>();
        collectionList = findByQuery("from Collection conn where conn.uid = ? and conn.goods_id = ?",uid, goods_id);
        try {
            if (collectionList.size() >0 ) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
